// JavaScript Document

<!--
// create ajaxObject supports all browser with XMLHttpRequest Support
ajaxObject = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
try { return new XMLHttpRequest() } catch(e){
throw new Error( "This browser does not support XMLHttpRequest." );
}}}}}}
var ajaxObj = new ajaxObject(); // use ajaxObject to start XMLHttpRequest() for most browsers
var purlObj = new ajaxObject();
var ajaxObj_onerrorupdate = false;
var ajaxObj_elementtype = 0;
//0 is the normal input

var ajaxObj_finishupdating = 1;	  var savebuttonvalue = '';

if(ajaxObj_requests == null){
	var ajaxObj_requests = new Array();
}



function handleResponse(timestampIndex) {
 	if(ajaxObj != null){
	    if(ajaxObj.readyState == 4){
	        var response = ajaxObj.responseText;
	        var update = new Array();
	        if(response.indexOf('||' != -1)) {
	            update = response.split('||');
				if(update[0] == '1' && update[3] == 'DONE'){
					if(ajaxObj_onerrorupdate){
						if(document.getElementById(update[1] + '_error') != null)
		            		document.getElementById(update[1] + '_error').innerHTML = update[2];
	            	}
	            	if(ajaxObj_elementtype == 1){
	            		//div
	            		document.getElementById(update[1]).innerHTML = update[2];
	            		
	            	}
	            	
	            	//reset to defaults;
	            	ajaxObj_elementtype = 0;
	            	ajaxObj_onerrorupdate = true;
				}			
	        }
			ajaxObj_finishupdating = 1;	        
			ajaxObj_requests[timestampIndex] = 2;					    var savebutton = document.getElementById("wizard_save_button");		    if(savebutton != null){		    	savebutton.value = savebuttonvalue;		    }
	    } else {
			setTimeout("handleResponse('"+timestampIndex+"')", 10);
		}
    }
}



function goNext( elementID,  stepID){    

  	div1 = document.getElementById(elementID + (stepID + ''));
	div1.style.display = 'none';

	div2 = document.getElementById(elementID + ((++stepID) + ''));
	if(div2 != null){
	   div2.style.display = 'block';
	} else {
	   document.getElementById("step_finish").style.display = 'block';
	}
  }


 function updateInput(sessionID, inputID, address, result, onerrorupdate, checkbox_reverse, a1b2, timestampIndex, special, checksum){
 	sessionID = sessionID + "";
 	inputID = inputID + "";
 	if(address == null) address = "";
 	address = address + ""; 	
	inputObj = document.getElementById(inputID);	if(! (inputObj == null || inputObj == "undefined")){		if(inputObj.type == "radio"){			var inputName_temp = inputID.split(":::");			var inputName = inputName_temp[0];			inputObj_Radio = document.getElementsByName(inputName);		}	}
//	if(checksum.length != 32){
//		throw "Invalid Checksum";
//	}

	if(special == "favourite" || special == 'favourite'){
		wizardID = "ecbe30f62319507c8e5917b23f34d0ab";
	} else if (special == "rating" || special == 'rating'){
		wizardID = "09bfd0b46c34f6e90487992c524e6fa9";
	} else if (special == "voting" || special == 'voting'){
	 	wizardID = "4c0c52731ffeb782d679ba82bc0fe353";
	} else {
		wizardID = document.getElementById("wizardID_DIV").innerHTML;
	}
	if(a1b2 == null || a1b2.length <= 0 || a1b2 == "undefined"){
		var a1b2;
		if(document.getElementById("a1b2") != null){
			a1b2 = document.getElementById("a1b2").innerHTML;
		}
	}
	if(timestampIndex == null || timestampIndex == "undefined"){
		timestampIndex = Math.floor(new Date().getTime()/1000);
	}
	
	if(result == null || result == "undefined"){
		if(inputObj.type == "checkbox"){
			if(inputObj.checked == true){
				if(checkbox_reverse != null && checkbox_reverse == "true"){
					result = "false";
				} else {
					result = "true";
				}
			}	else {
				if(checkbox_reverse != null && checkbox_reverse == "true"){
					result = "true";
				} else {
					result = "false";
				}
			}		} else if(inputObj.type == "radio"){			result = getCheckedValue(inputObj_Radio);		} else if(inputObj.type=="select-multiple") {			 result = "";			 for (i=0; i<inputObj.options.length; i++) {				    if (inputObj.options[i].selected) {				      result += inputObj.options[i].value +";";				      				    }			 }
		} else {			
		    result = inputObj.value;
		    if(result == null){
		    	result = inputObj.innerHTML;
		    }
		    if(result == null){		    	result = '';
		    }		    /*select-multiple*/
	    }
    }
    result = result + "";
	var re = new RegExp("&", "g");
    result = result.replace(re,"%26");
    re = new RegExp("=", "g");    
    result = result.replace(re, "%3D");    
    
    re = new RegExp("\n", "g");  //For TextArea
    result = result.replace(re, "%0A");
    
    if((ajaxObj.readyState > 0 && ajaxObj.readyState < 4) || ajaxObj_finishupdating == 0){    
    			ajaxObj_requests[timestampIndex] = 0;
				setTimeout("updateInput('"+ sessionID +"','"+ inputID +"','"+address +"','"+result+"','"+onerrorupdate+"','false','"+a1b2+"', '"+timestampIndex+"','"+special+"','"+checksum+"');", 10);
	    		if(ajaxObj_finishupdating == 0 && ajaxObj.readyState == 4){
	    			handleResponse(timestampIndex);
	    		}

				return timestampIndex;
	}
	
	ajaxObj_finishupdating = 0;

    if(onerrorupdate == null || onerrorupdate == true || onerrorupdate == "undefined"){
    	if(onerrorupdate == null) onerrorupdate = true;
    	if(document.getElementById(inputID + '_error') != null){
			document.getElementById(inputID + '_error').innerHTML = "<img src='http://www.mediaecho.com.sg/graphics/loadingAnimation.gif' width='20px' height='20px' />";
		} 
		ajaxObj_onerrorupdate = true;
	} else {
		ajaxObj_onerrorupdate = false;
	}    var savebutton = document.getElementById("wizard_save_button");    if(savebutton != null){    	savebuttonvalue = savebutton.value;    	savebutton.value = "Saving...Please wait.";    }    
	var params = 'wizardID=' + wizardID + '&sessionID=' + sessionID + '&inputID=' + inputID + '&result=' + result + '&a1b2=' + a1b2 + '&checksum=' + checksum + '&';
		ajaxObj.open('post', address + 'updateWizard.php');
		ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxObj.setRequestHeader("Content-length", params.length);
		ajaxObj.setRequestHeader("Connection", "close");
		
		ajaxObj.send(params);	
		ajaxObj_requests[timestampIndex] = 1;
		if( navigator.appName == "Microsoft Internet Explorer"){
			setTimeout("handleResponse('"+timestampIndex+"')", 10);
		} else {
			ajaxObj.onreadystatechange = handleResponse(timestampIndex);
		}
		return timestampIndex;
 }
 
 
 //Upload Files Functions
 function updateIFrameInput(iframeID, inputID)
 {
 	iframe = window.frames[iframeID];
// 	iframe_inputObj =  	iframeObj.document.getElementById(iframeInputID);
 //	iframe_submitObj = iframeObj.document.getElementById(iframeInputID +"_submit");
	doc = null;
	
   if(iframe.contentDocument)  
      // Firefox, Opera  
      doc = iframe.contentDocument;  
   else if(iframe.contentWindow)  
      // Internet Explorer  
      doc = iframe.contentWindow.document;  
   else if(iframe.document)  
      // Others?  
      doc = iframe.document;
   else 
	//Safari
	doc = iframe;  
  
	if(doc == null){
	  	iframe = document.getElementById(iframeID);
	  	if(iframe.contentDocument)  
	      // Firefox, Opera  
	      doc = iframe.contentDocument;  
	   else if(iframe.contentWindow)  
	      // Internet Explorer  
	      doc = iframe.contentWindow.document;  
	   else if(iframe.document)  
	      // Others?  
	      doc = iframe.document;  
	   else
		//Safari
		doc = iframe;
	  
  	}
  	
   if(doc != null)  {   
	 	inputObj = document.getElementById(inputID);
	 	var myDivObj = doc.getElementById('myDiv');
		if(myDivObj == null){
			alert("Upload not successful.");
		} else {
			if(isIE()){
			myDivObj.appendChild(inputObj);
			} else {
			myDivObj.appendChild(inputObj.cloneNode(true));
			}
		}
	 	if(isIE()){
	 	 	doc.getElementById("myForm").click();
	 	 } else {
			var myForm_temp = doc.getElementById("myForm");
			if(myForm_temp != null){
				myForm_temp.submit();
			} else {
				alert("Upload not successful.");
			}
		}
		if(document.getElementById(inputID + '_DIV_error') != null){
			document.getElementById(inputID + '_DIV_error').style.display = "block";
			document.getElementById(inputID + '_DIV_error').innerHTML = "<img src='http://www.mediaecho.com.sg/graphics/loadingAnimation.gif' width='20px' height='20px' />";
		}
	}
 }
 
 function showDIV_error(inputID){
 		if(document.getElementById(inputID + '_DIV_error') != null){
			document.getElementById(inputID + '_DIV_error').style.display = "block";
			document.getElementById(inputID + '_DIV_error').innerHTML = "<img src='http://www.mediaecho.com.sg/graphics/loadingAnimation.gif' width='20px' height='20px' />";
		}
 }
 
 function isIE()
 {
      return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
 }

function setValue(inputID, Value){
	document.getElementById(inputID).value = Value;
}

function limitValue(inputID, lowerLimit, upperLimit){
	var obj = document.getElementById(inputID);
	if(upperLimit != null && lowerLimit != null){
		if(upperLimit < lowerLimit){
			throw "UpperLimit < LowerLimit";
		}
	}
	if(obj == null){
		throw "Null Object";
	}	
	if(lowerLimit != null){
		if(parseFloat(obj.value) < parseFloat(lowerLimit)){
			alert("Value must not be smaller than " + (lowerLimit +""));
			return -1;
		}
	}
	if(upperLimit != null){
		if(parseFloat(obj.value) > parseFloat(upperLimit)){
			alert("Value must not be larger than " + (upperLimit +""));
			return 1;		
		}
	}
	return 0;
}

function limitValueAndUpdate(inputID, lowerLimit, upperLimit){
	var obj = document.getElementById(inputID);
	var limit_result = limitValue(inputID, lowerLimit, upperLimit);
	if(limit_result < 0){
		//Lower than lowerLimit
		obj.value = lowerLimit;
	} else if(limit_result > 0){
		//Higher than upperLimit
		obj.value = upperLimit;
	} else {
		//Within Limit
		//Do nothing
	}
	return true;
}

function checkAndHide(inputID)
{
	inputID = inputID + "";
	if(document.getElementById(inputID + "_error") != null){
		if(document.getElementById(inputID + "_error").innerHTML == "Updated!"){
			document.getElementById(inputID + "_text1").innerHTML = document.getElementById(inputID).value;
			showhide(inputID + "_edit");
			showhide(inputID + "_text");
		} else {
			setTimeout("checkAndHide('"+ inputID + "');",10);
		}
	}
}

function checkAndDoNotHide(inputID)
{
	inputID = inputID + "";
	if(document.getElementById(inputID + "_error") != null){
		if(document.getElementById(inputID + "_error").innerHTML == "Updated!"){
			if(document.getElementById(inputID + "_text1") != null){
				document.getElementById(inputID + "_text1").innerHTML = document.getElementById(inputID).value;
			}
	//		showhide(inputID + "_edit");
	//		showhide(inputID + "_text");
		} else {
			setTimeout("checkAndDoNotHide('"+ inputID + "');",10);
		}
	}
}


function showhide(id){
			obj= document.getElementById(id);
			if(obj == null) return;
			if (obj.style.display == "none"){
				obj.style.display = "block";
			} else {
				obj.style.display = "none";
			}

}

function show(id){
	if(document.getElementById(id) != null){
		document.getElementById(id).style.display = "block";
	}
}

function fade(id)
{
	if(document.getElementById){
		if(document.getElementById(id).style.display == "block"){
			setTimeout("showhide('"+ id + "');",5000);
		}
	}
}

//TextArea
function countLines(strtocount, cols) {
    var hard_lines = 1;
    var last = 0;
    while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
    }
    var soft_lines = Math.round(strtocount.length / (cols-1));
    var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
    if ( hard ) soft_lines = hard_lines;
    return soft_lines;
}

function textarea_adjust_height(obj){
	obj.rows = countLines(obj.value, obj.cols) +1;
}

function clearIt(oTxt){
  if (oTxt.value==oTxt.defaultValue){
    oTxt.value = "";
  }
}

function resetTxt(oTxt){
  if (oTxt.value==""){
    oTxt.value = oTxt.defaultValue;
  }
}

//Favourites Wizard Functions
function setFavourite(ID){
		imgObj = document.getElementById(ID);
		if(imgObj.src.search(/starfull.png/) >= 0){
			return -1;
		} 		
		return Math.floor(new Date().getTime()/1000);

}


function updateFavourite(ID, timestampIndex){
	if(ajaxObj_requests[timestampIndex] == 2){
		imgObj = document.getElementById(ID);
		if(imgObj.src.search(/starnil.png/) >= 0){													 
			imgObj.src = "http://www.mediaecho.com.sg/graphics/starfull.png";
		} else {
			imgObj.src = "http://www.mediaecho.com.sg/graphics/starnil.png";
		}
		return;
	} 
	setTimeout("updateFavourite('"+ID + "','"+timestampIndex+"');",50);	
}

//Rating Wizard Functions
function updateRating(ID, timestampIndex, rating, randomID2){
	var textupdateDIV = document.getElementById("text_" + randomID2);
	if(ajaxObj_requests[timestampIndex] == 2){
		for (n = 1; n <= 5; n++){
			imgObj = document.getElementById(ID + "_" + n );
			if(n <= rating){													 
				imgObj.src = "http://www.mediaecho.com.sg/graphics/starfull.png";
			} else {
				imgObj.src = "http://www.mediaecho.com.sg/graphics/starnil.png";
			}
		}
		textupdateDIV.innerHTML = "Thank you for rating!";
		return;
	} 
	textupdateDIV.innerHTML = "Updating...";
	setTimeout("updateRating('"+ID + "','"+timestampIndex+"', '"+rating+"' , '" + randomID2 + "');",50);	
}

//Voting Wizard Functions
function updateVoting(ID, timestampIndex, buttonID){
	if(ajaxObj_requests[timestampIndex] == 2){
		voteObj = document.getElementById(buttonID);
		voteObj.disabled = true;
		location.reload(true);
		return;
	} 
	setTimeout("updateVoting('"+ID + "','"+timestampIndex+"', '"+buttonID+"');",50);	
}

//PURL
function updatePURL(purl){
		var params = 'u=' + purl + '&';
		purlObj.open('post',  'http://www.mediaecho.com.sg/u/u.php');
		purlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		purlObj.setRequestHeader("Content-length", params.length);
		purlObj.setRequestHeader("Connection", "close");
		
		purlObj.send(params);	
		if( navigator.appName == "Microsoft Internet Explorer"){
			setTimeout("purl_handleResponse('"+purl+"')", 10);
		} else {
			purlObj.onreadystatechange = purl_handleResponse(purl);
		}
} 
function purl_handleResponse(purl){
	if(purlObj != null){
	    if(purlObj.readyState == 4){
	        var response = purlObj.responseText;
	        var error_response = document.getElementById("profile_purl_error");
	        if(response.length == 0){
	        	error_response.style.color = "white";
	        	error_response.innerHTML = "Updated.";
	        } else {
	        	error_response.style.color = "red";
	        	error_response.innerHTML = response;
	        }
	    } else {
			setTimeout("purl_handleResponse('"+purl+"')", 10);
		}
    }
}

function getCheckedValue(radioObj) {	if(!radioObj)		return "";	var radioLength = radioObj.length;	if(radioLength == undefined)		if(radioObj.checked)			return radioObj.value;		else			return "";	for(var i = 0; i < radioLength; i++) {		if(radioObj[i].checked) {			return radioObj[i].value;		}	}	return "";}


//-->
