// JavaScript Document
/*
	1 - My Profile
	2 - Change Password
	3 - Messages
	4 - Compose Message
	5 - Send to a Friend
	6 - 
	7 - Order Upload Files Matter
*/
function ddlRemoveDefaultString(obj) {
	if(obj.options[0]!=null) {
		if(obj.options[0].text.indexOf('Select')>=0) {
			obj.remove(0);
		}
	}
}
function strDurationBlur(evt, obj) {
	var no = parseInt(obj.value);
	if(!(no>=3 && no<=120)) {
		alert('The allowed number of days is 3 - 120');
		obj.value='0';
	}
	else {
		obj.value=no;
		if(obj.value=='NaN') {
			alert('Please input a proper number 3 - 120');
			obj.value = '0';
		}
		else {
			obj.value = no;
		}
	}
}
function ddlDurationChange(evt, obj) {
	var index=obj.selectedIndex;
	if(obj.options[index].text == 'Other')
		document.getElementById('strDuration').disabled=false;
	else
		document.getElementById('strDuration').disabled=true;
}
function memberPageLoad(num) {
	if(num==1) {
		if(document.getElementById('hdError').value=='1') {
			memberError(num);
			document.getElementById('hdError').value='';
		}
	}
	else if(num==2) {
		if(document.getElementById('hdError').value=='1') {
			memberError(num);
			document.getElementById('hdError').value='';
		}
	}
	else if(num==4) {
		textAreaLoad(document.getElementById('strDetail'), 1000, document.getElementById('divCounter'));
	}
	else if(num==5) {
		if(document.getElementById('hdError').value=='1') {
			memberError(num);
			document.getElementById('hdError').value='';
		}
	}
	else if(num==6) {
	}
	else if(num==7) {
		if(document.getElementById('hdError').value=='1') {
			memberError('7a');
		}
		else if(document.getElementById('hdError').value=='2') {
			memberError('7b');
		}
		else { }
		document.getElementById('hdError').value='';
		var objFrm=parent.document.getElementById('frmUploadFiles');
		objFrm.height=document.documentElement.scrollHeight+'px';
	}
	else if(num==8) {
		var objStatus;
		var x;
		var objStatusId;
		x=0;
		objStatusId='lblStatus_';
		objStatus=document.getElementById(objStatusId+x);
		while(objStatus!=null) {
			if(objStatus.innerHTML.indexOf('Payment Pending')>=0) {
				objStatus.style.color='#FF0000';
			}
			x++;
			objStatus=document.getElementById(objStatusId+x);			
		}
	}
	else if(num==9) {
		var objStatus;
		var x;
		var objStatusId;
		x=0;
		objStatusId='lblStatus_';
		objStatus=document.getElementById(objStatusId+x);
		while(objStatus!=null) {
			if(objStatus.innerHTML.indexOf('Payment Pending')>=0) {
				objStatus.style.color='#FF0000';
			}
			x++;
			objStatus=document.getElementById(objStatusId+x);			
		}
	}	
	else {
	}
}
function memberInfoSubmit(num) {
	var status=true;
	var objDivError;
	var objTdDiv=document.getElementById('tdDivError');
	var objTable;
	var objTr;
	var objTd;
	var attr;
	var index;
	var indexText;
	var errorHtml='';
	var phoneRegExp=/^([0-9])+-+([0-9])+-+([0-9])+$/;
	var emailRegExp=/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
	if(objTdDiv.childNodes[0]!=null)
		objTdDiv.removeChild(objTdDiv.childNodes[0]);
	objTable=document.createElement('table');
	objTr=document.createElement('tr');
	objTd=document.createElement('td');
	objTable.setAttribute('border','0');
	objTable.setAttribute('cellPadding','5');
	objTable.setAttribute('cellSpacing','0');
	objTable.setAttribute('style','visibility: visible;');
	objTd.setAttribute('align','left');
	objTr.appendChild(objTd);
	objTable.appendChild(objTr);
	//
	objDivError=document.createElement('div');
	objDivError.setAttribute('style','width: 400px; visibility: visible; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 9px; color: #FF0000;');
	//
	//Checking for errors
	errorHtml="<span style='font-size: 10px;'>Following errors were found while filling the form:</span>";
	errorHtml+="<ul>";
	//Start checking Errors for 1
	if(num==1) {
		if(trim(document.getElementById('strFirstName').value)=='') {
			errorHtml+="<li>First name can not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strLastName').value)=='') {
			errorHtml+="<li>Last name can not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strCity').value)=='') {
			errorHtml+="<li>City can not be left blank</li>";
			status=false;
		}
		index=document.getElementById('ddlCountry').selectedIndex;
		indexText=document.getElementById('ddlCountry').options[index].text;
		if(indexText.indexOf('Select')>=0) {
			errorHtml+="<li>Please select your country</li>";
			status=false;
		}
		if(trim(document.getElementById('strContact1').value)=='') {
			errorHtml+="<li>Contact 1 can not be left blank</li>";
			status=false;
		}
		else {
			if(!phoneRegExp.test(document.getElementById('strContact1').value)) {
				errorHtml+="<li>Invalid Contact 1</li>";
				status=false;
			}
		}
		if(trim(document.getElementById('strEmail').value)=='') {
			errorHtml+="<li>Email can not be left blank</li>";
			status=false;
		}
		else {
			if(!emailRegExp.test(document.getElementById('strEmail').value)) {
				errorHtml+="<li>Invalid Email</li>";
				status=false;
			}
		}
	}
	else if(num==2) {
		if(trim(document.getElementById('strOldPassword').value)=='') {
			errorHtml+="<li>Old password can not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strPassword').value)=='') {
			errorHtml+="<li>Password can not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strPassword').value)!=trim(document.getElementById('strConfirmPassword').value)) {
			errorHtml+="<li>Password and Confirm password must match</li>";
			status=false;
		}
		
	}
	else if(num==4) {
		index=document.getElementById('ddlOrder').selectedIndex;
		if(index!=-1) {
			indexText=document.getElementById('ddlOrder').options[index].text;
			if(indexText.indexOf('Select')>=0) {
				errorHtml+="<li>Please select order id</li>";
				status=false;
			}
		}
		else {
			errorHtml+="<li>Please select order id</li>";
			status=false;
		}
		if(trim(document.getElementById('strSubject').value)=='') {
			errorHtml+="<li>Subject can not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strDetail').value)=='') {
			errorHtml+="<li>Details can not be left blank</li>";
			status=false;
		}
	}
	else if(num==5) {
		index=document.getElementById('ddlDuration').selectedIndex;
		indexText=document.getElementById('ddlDuration').options[index].text;
		if(indexText.indexOf('Select')>=0) {
			errorHtml+="<li>Please select duration in days</li>";
			status=false;
		}
		else if(indexText.indexOf('Other')>=0) {
			if(document.getElementById('strDuration').value=='' || document.getElementById('strDuration').value=='0') {
				errorHtml+="<li>Duration in days can not be zero or blank</li>";
				status=false;
			}
		}
	}
	else if(num==6) {
		if(trim(document.getElementById('strFirstName').value)=='') {
			errorHtml+="<li>Friend's first name not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strLastName').value)=='') {
			errorHtml+="<li>Friend's last name not be left blank</li>";
			status=false;
		}
		if(trim(document.getElementById('strEmail').value)=='') {
			errorHtml+="<li>Email can not be left blank</li>";
			status=false;
		}
		else {
			if(!emailRegExp.test(document.getElementById('strEmail').value)) {
				errorHtml+="<li>Invalid Email</li>";
				status=false;
			}
		}
	}
	//End checing Errors
	errorHtml+="</ul>";
	//
	if(errorHtml.indexOf('<li>')>=0) {
		if(document.all) {
			objTdDiv.innerHTML = "<table border='0' cellpadding='5' cellspacing='0'>" +
				"<tr><td align='left' valign='top' style='font-family: Verdana, Arial, Helvetica,"+
				" sans-serif; font-weight: bold; font-size: 9px; color: #FF0000; color: red;'>" +
				errorHtml + "</td></tr></table>";
		}
		else {
			objDivError.innerHTML=errorHtml;
			objTd.appendChild(objDivError);
			objTdDiv.appendChild(objTable);
		}
	}
	document.documentElement.scrollTop='0px';
	if(status) {
		if(num==1) {
	 		document.getElementById('accountInfoForm').submit();
		}
		else if(num==2) {
			document.getElementById('changePasswordForm').submit();
		}
		else if(num==4) {
			document.getElementById('composeMessageForm').action='includePages/composeMessageMatterProcess.php';
			document.getElementById('composeMessageForm').submit();
		}
		else if(num==5) {
			document.getElementById('viewOrderForm').submit();
		}
		else if(num==6) {
			document.getElementById('sendToFriendForm').submit();
		}

	}
}
function memberError(num) {
	var objDivError;
	var objTdDiv=document.getElementById('tdDivError');
	var objTable;
	var objTr;
	var objTd;
	var attr;
	var index;
	var indexText;
	var errorHtml='';
	var phoneRegExp=/^([0-9])+-+([0-9])+-+([0-9])+$/;
	var emailRegExp=/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
	if(objTdDiv.childNodes[0]!=null)
		objTdDiv.removeChild(objTdDiv.childNodes[0]);
	objTable=document.createElement('table');
	objTr=document.createElement('tr');
	objTd=document.createElement('td');
	objTable.setAttribute('border','0');
	objTable.setAttribute('cellPadding','5');
	objTable.setAttribute('cellSpacing','0');
	objTable.setAttribute('style','visibility: visible;');
	objTd.setAttribute('align','left');
	objTr.appendChild(objTd);
	objTable.appendChild(objTr);
	//
	objDivError=document.createElement('div');
	objDivError.setAttribute('style','width: 400px; visibility: visible; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 9px; color: #FF0000;');
	//
	//Checking for errors
	errorHtml="<span style='font-size: 10px;'>Following errors were found while filling the form:</span>";
	errorHtml+="<ul>";
	//Start checking Errors
	if(num==1) {
		errorHtml+="<li>Email Address already exists</li>";
	}
	else if(num==2) {
		errorHtml+="<li>Old Password not valid</li>";
	}
	else if(num==5) {
		errorHtml+="<li>Update duration can not be lesser than the last one, please contact admin.</li>";		
	}
	else if(num=='7a') {
		errorHtml+="<li>There was an error while uploading file.</li>";		
	}
	else if(num=='7b') {
		errorHtml+="<li>Your file size must be lesser than 1 MB or your total 1 MB qouta is exceeding.</li>";		
	}
	else {}
	//End checing Errors
	errorHtml+="</ul>";
	//
	if(errorHtml.indexOf('<li>')>=0) {
		if(document.all) {
			objTdDiv.innerHTML = "<table border='0' cellpadding='5' cellspacing='0'>" +
				"<tr><td align='left' valign='top' style='font-family: Verdana, Arial, Helvetica,"+
				" sans-serif; font-weight: bold; font-size: 9px; color: #FF0000; color: red;'>" +
				errorHtml + "</td></tr></table>";
		}
		else {
			objDivError.innerHTML=errorHtml;
			objTd.appendChild(objDivError);
			objTdDiv.appendChild(objTable);
		}
	}
	document.documentElement.scrollTop='0px';
}
function ddlOrderIdChange(obj) {
	var frm=document.getElementById('composeMessageForm');	
	var index=obj.selectedIndex;
	var selOrderId=obj.options[index].text;
	frm.action='composeMessage.php?oid=' + selOrderId;
	frm.submit();
}