function resize_and_center_window(winW,winH){
        var w = screen.width, h = screen.height;

        if (document.all || document.layers) {
           w = screen.availWidth;
           h = screen.availHeight;
        }

        var popW = winW, popH = winH;

        var leftPos = (w-popW)/2, topPos = (h-popH)/2;


        window.resizeTo(popW,popH)
        window.moveTo(leftPos,topPos)
}

//validate email
function isValidEmail(str) {
       return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

//validate SG phone number
function isValidPhone(num, locale){
	var mobile_pattern_sg = /[689]\d{7}/;

    if (num && !mobile_pattern_sg.test(num) && locale=="SG") {
        alert("Please enter valid Contact number.");
        return false;
    }else{
		return true;
	}

}

//for resetting of password
function validate_reset_pwd_form(locale){
	//brief basic check for values
	if(document.getElementById('usrName').value==""){
		alert('Please enter your name!');
		document.getElementById('usrName').focus();
		return false;
	}
	if(document.getElementById('usrUID').value==""){
		alert('Please enter your NRIC!');
		document.getElementById('usrUID').focus();
		return false;
	}	
	if(document.getElementById('usrClass').value==""){
		alert('Please enter your Class!');
		document.getElementById('usrClass').focus();
		return false;
	}	
	if(document.getElementById('usrSch').value==""){
		alert('Please enter your School!');
		document.getElementById('usrSch').focus();
		return false;
	}	
	if(document.getElementById('usrEmail').value==""){
		alert('Please enter your Email address!');
		document.getElementById('usrEmail').focus();
		return false;
	}else{
		//check validity
		var emailvalid=isValidEmail(document.getElementById('usrEmail').value);
        if(!emailvalid){
            alert('Sorry, invalid email format detected!');
            return false;
        }
	}	
	if(document.getElementById('usrContact').value==""){
		alert('Please enter your Contact number!');
		document.getElementById('usrContact').focus();
		return false;
	}else{
		var phonevalid=isValidPhone(document.getElementById('usrContact').value,locale);
		if(!phonevalid){
			document.getElementById('usrContact').focus();
			return false;
		}
	}

//	document.getElementById('pwdresetform').submit();
	document.getElementById('submitbtn').disabled = true;
	document.getElementById('act').value="reset_pwd";
	document.getElementById('action_type').value="reset_pwd_request";
	//document.pwdresetform.submit();
	document.getElementById('pwdresetform').submit();
}

function validateFile(filename){
        var filetype="";
        var mimetype="";
        var filename_only="";
		var status=0;
		if(filename){	
                //change to find last occurrance of the dot
	            filetype=filename.substring(filename.lastIndexOf('.') + 1,filename.length);
                filetype = filetype.toLowerCase();

                filename_only=filename.substring(filename.lastIndexOf('\\') + 1,filename.length-filetype.length-1);

                if(filename_only){
                    //propagate downwards
                    status=mapResourceType(filetype);
                }

				if(status){
				}else{
					alert('Sorry, this file format is not supported! Please upload only images or word doc');
					document.getElementById('import_file').value="";
					return false;
				}
		}
}

function mapResourceType(mfiletype){

        if(mfiletype == 'jpg' || mfiletype == 'bmp' || mfiletype == 'png' || mfiletype == 'gif' || mfiletype == 'jpeg'){
            //document.f.resource_type.selectedIndex=4; // Still Image
			return 1;
        }else if(mfiletype=='doc' || mfiletype=='ppt' || mfiletype=='txt' || mfiletype=='pdf'){
			return 1;
        }else{
			return 0;
		}
}

function validateHelpForm(){
	//validate the stuffs
	if(document.getElementById('usrQTitle').value==""){
		alert("Please enter a query title!");
		document.getElementById('usrQTitle').focus();
		return false;
	}
	if(document.getElementById('usrName').value==""){
		alert("Come on, tell us your name.");
		document.getElementById('usrName').focus();
		return false;
	}
	if(document.getElementById('usrSch').value==""){
		alert("Tell us which school you're in.");
		document.getElementById('usrSch').focus();
		return false;
	}
	if(document.getElementById('usrClass').value==""){
		alert("Tell us which class you're in.");
		document.getElementById('usrClass').focus();
		return false;
	}
	if(document.getElementById('usrUID').value==""){
		alert("Please key in your BC/NRIC.");
		document.getElementById('usrUID').focus();
		return false;
	}
	if(document.getElementById('usrContact').value==""){
		alert("Please key in your contact number");
		document.getElementById('usrContact').focus();
		return false;
    }else{
	/*
        var phonevalid=isValidPhone(document.getElementById('usrContact').value,'SG');
        if(!phonevalid){
            document.getElementById('usrContact').focus();
            return false;
        }
	*/
    }
	
	if(document.getElementById('usrEmail').value==""){
		alert("Please key in your email address.");
		document.getElementById('usrEmail').focus();
		return false;
    }else{
        //check validity
        var emailvalid=isValidEmail(document.getElementById('usrEmail').value);
        if(!emailvalid){
            alert('Sorry, invalid email format detected!');
			document.getElementById('usrEmail').focus();
            return false;
        }
    }
	
		
	if(document.getElementById('usrDesc').value==""){
		alert("Tell us what is the issue, and the sequence of events that leads to the issue.");
		document.getElementById('usrDesc').focus();
		return false;
	}
	
	//once everything is beautiful

    document.getElementById('submitbtn').disabled = true;
    document.getElementById('act').value="help";
    document.getElementById('action_type').value="help_request_submit";

	//document.helpform.submit();
	document.getElementById('helpform').submit();
}

